﻿using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace RigLib
{
    class WallChanger
    {
        [DllImport("user32.dll")]
        public static extern Boolean SetSysColors(
            Int32 cElements,
            Int32[] lpaElements,
            Int32[] lpaRgbValues
            );

        const Int32 COLOR_BACKGROUND = 1;


        /* #define RGB(r,g,b)          
         * ((COLORREF)(((BYTE)(r)|
         * ((WORD)((BYTE)(g))<<8))|
         * (((DWORD)(BYTE)(b))<<16)))
         */

        /// <summary>
        /// Wingdi.h = shifts buts to create a "COLORREF"
        /// </summary>
        /// <param name="r">Red byte</param>
        /// <param name="g">Green byte</param>
        /// <param name="b">Blue byte</param>
        /// <returns>OR'd RGB value</returns>
        static Int32 RGB(byte r, byte g, byte b)
        {
            int rbg = r | g << 8 | b << 16;

            return rbg;
        }

        static void Main(string[] args)
        {
            int[] aiElements = { COLOR_BACKGROUND };
            byte[] bytes = new byte[4];

            while (true)
            {

                /** get red **/
                Random rand = new Random();
                rand.NextBytes(bytes);

                byte r = bytes[3];
                byte g = bytes[0];
                byte b = (byte)rand.Next(1);

                /** call RGB to get COLORREF **/
                Int32[] aColors = { RGB(r, g, b) };

                /** Set background **/
                SetSysColors(1, aiElements, aColors);

                /** recycle **/
                Thread.Sleep(15000);
            }
        }
    }
}
